package com.zc.ucenter.controller;


import com.zc.ucenter.entity.vo.RegisterVo;
import com.zc.ucenter.service.MemberService;
import com.zc.utils.JwtUtils;
import com.zc.utils.R;
import com.zc.utils.vo.Member;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author cong
 * @since 2022-12-01
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
public class MemberController {

	@Autowired
	private MemberService memberService;

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	// 登录
	@PostMapping("/login")
	public R login(@RequestBody Member member) {
		String token = memberService.login(member);
		return R.ok().data("token", token);
	}

	// 注册
	@PostMapping("/register")
	public R register(@RequestBody RegisterVo registerVo) {
		boolean isRegister = memberService.registerUser(registerVo);
		return isRegister ? R.ok().data("msg", "注册成功") : R.error().data("msg", "注册失败");
	}

	// 根据token获取用户信息
	@GetMapping("getMember")
	public R getMember(HttpServletRequest request) {
		String id = JwtUtils.getMemberIdByJwtToken(request);

		if (!StringUtils.isEmpty(id)) {
			Member member = memberService.getById(id);
			return R.ok().data("member", member);
		}
		return R.ok().data("member", null);
	}

	// 根据用户Id获取用户xinxi
	@GetMapping("/getMemberInfo/{id}")
	public Member getMemberInfo(@PathVariable String id) {
		return memberService.getById(id);
	}
}

